package com.lease.web.admin.custom.converter;

import com.lease.model.enums.BaseEnum;
import org.springframework.core.convert.converter.Converter;
import org.springframework.core.convert.converter.ConverterFactory;

public class StringToBaseEnumConverterFactory implements ConverterFactory<String, BaseEnum> {
    @Override
    public <T extends BaseEnum> Converter<String, T> getConverter(Class<T> targetType) {
        return new Converter<String, T>() {
            @Override
            public T convert(String source) {

                for (T enumConstant : targetType.getEnumConstants()) {

                    if (Integer.parseInt(source) == enumConstant.getCode()) {
                        return enumConstant;
                    }

                }
              throw  new RuntimeException("传入参数和枚举类不匹配");

            }
        };
    }
}
